$(document).ready   B
last analyzed

Complexity

Conditions 1
Paths 128

Size

Total Lines 181

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 128
nop 0
dl 0
loc 181
rs 8.1463

9 Functions

Rating   Name   Duplication   Size   Complexity  
A 0 6 1
A 0 8 1
A 0 10 1
A 0 9 1
A 0 14 2
A 0 14 1
A 0 6 2
A 0 12 2
B 0 89 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
$(document).ready(function () {
2
    function closeDoorhanger() {
3
        $('#password-doorhanger').slideUp(function () {
4
            API.runtime.sendMessage(API.runtime.id, {
5
                method: "passToParent",
6
                args: {'injectMethod': 'closeDoorhanger'}
7
            });
8
        });
9
    }
10
11
    function resizeIframe(height) {
12
        API.runtime.sendMessage(API.runtime.id, {
13
            method: "passToParent",
14
            args: {'injectMethod': 'resizeIframe', args: height}
15
        });
16
    }
17
18
    var default_account;
19
    var dh = $('#password-doorhanger');
20
    var btn_config = {
21
        'cancel': function () {
22
            return {
23
                text: API.i18n.getMessage('cancel'),
24
                onClickFn: function () {
25
                    closeDoorhanger();
26
                    API.runtime.sendMessage(API.runtime.id, {method: "clearMined"});
27
                }
28
            };
29
        },
30
        'save': function (data) {
31
            var save = API.i18n.getMessage('save');
32
            var update = API.i18n.getMessage('update');
33
            var btnText = (data.guid === null) ? save : update;
34
            return {
35
                text: btnText,
36
                onClickFn: function (account) {
37
                    API.runtime.sendMessage(API.runtime.id, {method: "saveMined", args: {account: account}});
38
                    dh.find('.toolbar-text').text(API.i18n.getMessage('saving_to', [account.vault.name]) + '...');
39
                    dh.find('.passman-btn').hide();
40
                },
41
                isCreate: (data.guid === null)
42
            };
43
        },
44
        'updateUrl': function (data) {
45
            return {
46
                text: 'Update',
47
                onClickFn: function () {
48
                    API.runtime.sendMessage(API.runtime.id, {method: "updateCredentialUrl", args: data});
49
                    dh.find('.toolbar-text').text(API.i18n.getMessage('saving'));
50
                    dh.find('.passman-btn').hide();
51
                }
52
            };
53
        },
54
        'ignore': function (data) {
55
            return {
56
                text: API.i18n.getMessage('ignore_site'),
57
                onClickFn: function () {
58
                    //closeToolbar();
59
                    API.runtime.sendMessage(API.runtime.id, {method: "ignoreSite", args: data.currentLocation});
60
                    dh.find('.toolbar-text').text(API.i18n.getMessage('site_ignored'));
61
                    dh.find('.passman-btn').hide();
62
                    setTimeout(function () {
63
                        closeDoorhanger();
64
                    }, 3000);
65
                }
66
            };
67
        }
68
    };
69
70
    API.runtime.sendMessage(API.runtime.id, {method: "getRuntimeSettings"}).then(function (settings) {
71
        var accounts = settings.accounts;
72
        default_account = accounts[0];
73
        API.runtime.sendMessage(API.runtime.id, {method: "getDoorhangerData"}).then(function (data) {
74
            if (!data) {
75
                return;
76
            }
77
            var buttons = data.buttons;
78
            data = data.data;
79
            var username = (data.username) ? data.username : data.email;
80
            var doorhanger_div = $('<div id="password-toolbar" style="display: none;">');
81
            var text = data.selfAdded ? API.i18n.getMessage('credential_saved') : data.title + ' ' + username + ' at ' + data.url;
82
            $('<span>', {
83
                class: 'toolbar-text',
84
                text: text
85
            }).appendTo(doorhanger_div);
86
87
            $.each(buttons, function (k, button) {
88
                var btn = button;
89
90
                button = btn_config[btn](data);
91
                var html_button;
92
93
                if (btn === 'save') {
94
                    var btn_text = (button.isCreate && accounts.length > 1) ? API.i18n.getMessage('save_to','') : API.i18n.getMessage('save');
95
                    btn_text = (!button.isCreate) ? API.i18n.getMessage('update') : btn_text;
96
                    html_button = $('<button class="passman-btn btn-save btn-success"></button>').append('<span class="btn-txt"></span>');
97
                    html_button.find('.btn-txt').text(btn_text);
98
                    html_button.click(function () {
99
                        button.onClickFn(default_account);
100
                    });
101
102
                    if (button.isCreate && accounts.length > 1) {
103
                        var caret_container =  $('<span class="passman-btn caret-container"></span>').append('<span class="caret-container-txt"></span>');
104
                        caret_container.find('.caret-container-txt').text(default_account.vault.name);
105
                        var caret = $('<span class="fa fa-caret-down" style="margin-left: 5px; cursor: pointer;"></span>');
106
                        var menu = $('<div class="select_account" style="display: none;"></div>');
107
                        caret_container.append(caret);
108
                        doorhanger_div.append(caret_container);
109
                        for (var i = 1; i < accounts.length; i++) {
110
                            var a = accounts[i];
111
                            var item = $('<div class="account"></div>').text(API.i18n.getMessage('save_to', [a.vault.name]));
112
                            /* jshint ignore:start */
113
                            (function (account, item) {
114
                                item.click(function (e) {
115
                                    e.stopPropagation();
116
                                    e.preventDefault();
117
                                    button.onClickFn(account);
118
                                });
119
                            })(a, item);
120
                            /* jshint ignore:end */
121
                            menu.append(item);
122
                        }
123
                        caret_container.click(function (e) {
124
                            e.stopPropagation();
125
                            e.preventDefault();
126
                            var isVisible = ($('.select_account').is(':visible'));
127
                            var height = (isVisible) ? 0 : accounts.length * 29;
128
                            if (!isVisible) {
129
                                resizeIframe(height);
130
                            }
131
                            menu.slideToggle(function () {
132
                                if(isVisible){
133
                                    resizeIframe(height);
134
                                }
135
                            });
136
                        });
137
                        caret.after(menu);
138
                    }
139
140
                } else {
141
                    html_button = $('<button></button>',
142
                        {
143
                            class: 'passman-btn btn-'+ btn
144
                        }
145
                    ).append('<span class="btn-text"></span>');
146
                    html_button.find('.btn-text').text(button.text);
147
                    html_button.click(function () {
148
                        button.onClickFn();
149
                    });
150
151
                }
152
                doorhanger_div.append(html_button);
153
154
            });
155
            dh.html(doorhanger_div);
156
            doorhanger_div.slideDown();
157
        });
158
    });
159
    var _this = {};
160
161
    function minedLoginSaved(args) {
162
        // If the login added by the user then this is true
163
164
        var saved = API.i18n.getMessage('credential_saved');
165
        var updated = API.i18n.getMessage('credential_updated');
166
        var action = (args.updated) ? updated : saved;
167
        $('#password-toolbar').find('.toolbar-text').text(action + '!');
168
        setTimeout(function () {
169
            closeDoorhanger();
170
        }, 2500);
171
172
    }
173
174
    _this.minedLoginSaved = minedLoginSaved;
175
    API.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
0 ignored issues
show
Unused Code introduced by
The parameter sendResponse is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
176
        //console.log('Method call', msg.method);
177
        if (_this[msg.method]) {
178
            _this[msg.method](msg.args, sender);
179
        }
180
    });
181
});